import request from '@/utils/http'

// 数据权限规则响应结构
export interface DataPermissionRuleResponse {
  id?: number
  name: string
  rule_code: string
  table_name: string
  logical_operator: string
  condition_type: string
  config_json: Record<string, any>
  description?: string | null
  status?: boolean
  create_time?: string
  update_time?: string
}

// 数据权限规则列表响应
export interface DataPermissionRuleListResponse {
  total: number
  items: DataPermissionRuleResponse[]
  page: number
  size: number
  pages: number
}

// 角色数据权限关联响应结构
export interface RoleDataPermissionResponse {
  id: number
  role_id: number
  rule_id: number
  status: boolean
  create_time?: string
  update_time?: string
}

// 角色数据权限关联列表响应
export interface RoleDataPermissionListResponse {
  total: number
  items: RoleDataPermissionResponse[]
  page: number
  size: number
  pages: number
}

// 角色数据权限规则响应
export interface RoleDataPermissionRulesResponse {
  role_id: number
  rule_ids: number[]
}

// 创建/更新请求参数
export interface DataPermissionRuleCreate {
  rule_code: string
  name: string
  table_name: string
  logical_operator?: string
  condition_type: string
  description?: string | null
  status?: boolean
}

export interface DataPermissionRuleUpdate {
  name?: string
  table_name?: string
  logical_operator?: string
  condition_type?: string
  description?: string | null
  status?: boolean
}

export interface RoleDataPermissionCreate {
  role_id: number
  rule_id: number
}

export interface RoleDataPermissionUpdate {
  role_id?: number
  rule_id?: number
}

export interface RoleDataPermissionBatchCreate {
  role_id: number
  rule_ids: number[]
}

// 查询参数
export interface DataPermissionRuleQuery {
  current?: number
  size?: number
  rule_name?: string
  table_name?: string
  rule_type?: string
  is_active?: boolean
  keyword?: string
}

export interface RoleDataPermissionQuery {
  current?: number
  size?: number
  role_id?: number
  rule_id?: number
}

// ==================== 数据权限规则相关接口 ====================

/**
 * 创建数据权限规则
 */
export function createDataPermissionRule(params: DataPermissionRuleCreate) {
  return request.post<DataPermissionRuleResponse>({
    url: '/system/data-permissions/rules/create',
    params,
    showSuccessMessage: true
  })
}

/**
 * 获取数据权限规则列表
 */
export function fetchDataPermissionRuleList(params: DataPermissionRuleQuery) {
  return request.post<DataPermissionRuleListResponse>({
    url: '/system/data-permissions/rules/list',
    params
  })
}

/**
 * 获取数据权限规则详情
 */
export function fetchDataPermissionRule(ruleId: number) {
  return request.get<DataPermissionRuleResponse>({
    url: `/system/data-permissions/rules/${ruleId}`
  })
}

/**
 * 更新数据权限规则
 */
export function updateDataPermissionRule(ruleId: number, params: DataPermissionRuleUpdate) {
  return request.put<DataPermissionRuleResponse>({
    url: `/system/data-permissions/rules/${ruleId}`,
    params,
    showSuccessMessage: true
  })
}

/**
 * 删除数据权限规则
 */
export function deleteDataPermissionRule(ruleId: number) {
  return request.del<{ message: string }>({
    url: `/system/data-permissions/rules/${ruleId}`,
    showSuccessMessage: true
  })
}

/**
 * 根据表名获取数据权限规则
 */
export function fetchDataPermissionRulesByTable(tableName: string) {
  return request.get<DataPermissionRuleResponse[]>({
    url: `/system/data-permissions/rules/table/${tableName}`
  })
}

// ==================== 角色数据权限关联相关接口 ====================

/**
 * 创建角色数据权限关联
 */
export function createRoleDataPermission(params: RoleDataPermissionCreate) {
  return request.post<RoleDataPermissionResponse>({
    url: '/system/data-permissions/role-permissions/create',
    params,
    showSuccessMessage: true
  })
}

/**
 * 批量创建角色数据权限关联
 */
export function batchCreateRoleDataPermissions(params: RoleDataPermissionBatchCreate) {
  return request.post<RoleDataPermissionResponse[]>({
    url: '/system/data-permissions/role-permissions/batch',
    params,
    showSuccessMessage: true
  })
}

/**
 * 获取角色的数据权限规则ID列表
 */
export function fetchRoleDataPermissions(roleId: number) {
  return request.get<RoleDataPermissionRulesResponse>({
    url: `/system/data-permissions/role-permissions/role/${roleId}`
  })
}

/**
 * 获取角色关联的数据权限规则详情
 */
export function fetchRoleDataPermissionRules(roleId: number) {
  return request.get<DataPermissionRuleResponse[]>({
    url: `/system/data-permissions/role-permissions/rules/${roleId}`
  })
}

/**
 * 删除角色的所有数据权限关联
 */
export function deleteRoleDataPermissions(roleId: number) {
  return request.del<{ message: string }>({
    url: `/system/data-permissions/role-permissions/role/${roleId}`,
    showSuccessMessage: true
  })
}

/**
 * 获取角色数据权限关联列表
 */
export function fetchRoleDataPermissionList(params: RoleDataPermissionQuery) {
  return request.post<RoleDataPermissionListResponse>({
    url: '/system/data-permissions/role-permissions/list',
    params
  })
}

/**
 * 更新角色数据权限关联
 */
export function updateRoleDataPermission(permissionId: number, params: RoleDataPermissionUpdate) {
  return request.put<RoleDataPermissionResponse>({
    url: `/system/data-permissions/role-permissions/${permissionId}`,
    params,
    showSuccessMessage: true
  })
}

/**
 * 获取使用指定规则的角色关联列表
 */
export function fetchRuleRolePermissions(ruleId: number) {
  return request.get<RoleDataPermissionResponse[]>({
    url: `/system/data-permissions/role-permissions/rule/${ruleId}`
  })
}

/**
 * 删除单个角色数据权限关联
 */
export function deleteRoleDataPermission(permissionId: number) {
  return request.del<{ message: string }>({
    url: `/system/data-permissions/role-permissions/${permissionId}`,
    showSuccessMessage: true
  })
}